package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class TrackInfoServiceImpl extends ServiceImpl<TrackInfoMapper, TrackInfo> implements TrackInfoService {

	@Autowired
	private TrackInfoMapper trackInfoMapper;
	@Autowired
	private VodService vodService;

	@Autowired
	private AlbumInfoMapper albumInfoMapper;

	@Autowired
	private TrackStatMapper trackStatMapper;



	//根据专辑id查询专辑声音数据
	@Override
	public IPage<AlbumTrackListVo>
	findAlbumTrackPage(Page<AlbumTrackListVo> pageParam,
					   Long albumId, Long userId) {
		//根据专辑id分页查询得到数据
		IPage<AlbumTrackListVo> pageInfo =
				trackInfoMapper.findAlbumTrackPage(pageParam,albumId);

		//1 根据userId判断当前是否登录
		//1.1 如果没有登录
		//如果专辑不是免费的，可以试看的声音免费的，其他收费
		// isShowPaidMark=flase免费的  isShowPaidMark=true收费的

		//1.2 如果登录状态
		//判断如果专辑vip免费的
		//* 如果用户没有开通vip，     收费
		//* 如果用户开通VIP但是过期了，收费

		//判断如果专辑付费 收费
		//* 判断当前用户是否购买专辑，购买过，免费
		// 如果用户购买整个专辑，专辑里面所有声音免费
		// 如果用户只是购买了专辑某些声音，购买的声音免费的，其他收费

		return pageInfo;
	}






	@Override
	public void saveTrackInfo(TrackInfoVo trackInfoVo) {
		//1.添加声音基本信息 track_info
		TrackInfo trackInfo = new TrackInfo();
		BeanUtils.copyProperties(trackInfoVo,trackInfo);
		//手动设置
		//用户 id
		trackInfo.setUserId(1L);
		//声音在专辑中的排序值
		//获取专辑下声音数量order_num
		LambdaQueryWrapper<TrackInfo> queryWrapper = new LambdaQueryWrapper<>();
		//设置查询字段
		queryWrapper.select(TrackInfo::getOrderNum);
		//专辑id
		queryWrapper.eq(TrackInfo::getAlbumId,trackInfoVo.getAlbumId());
		//根据声音id降序
		queryWrapper.orderByDesc(TrackInfo::getId);
		//获取第一条数据
		queryWrapper.last(" limit 1");
		TrackInfo trackInfo_ordernum = trackInfoMapper.selectOne(queryWrapper);
		if (trackInfo_ordernum != null){
			trackInfo.setOrderNum(trackInfo_ordernum.getOrderNum() + 1);
		}else {
			trackInfo.setOrderNum(1);
		}

		//声音其他信息，时长，大小，类型等
		//要到腾讯云上获取
		//根据声音的media_file_id调用腾讯云方法获取
		TrackMediaInfoVo trackMediaInfoVo =
				vodService.getmediaInfoByFileId(trackInfoVo.getMediaFileId());
		//赋值给trackInfo
		 trackInfo.setMediaDuration(trackMediaInfoVo.getDuration());
		 trackInfo.setMediaSize(trackMediaInfoVo.getSize());
		 trackInfo.setMediaType(trackMediaInfoVo.getType());
		 trackInfo.setMediaUrl(trackMediaInfoVo.getMediaUrl());
		 //调用方法添加
		trackInfoMapper.insert(trackInfo);




		//2.操作专辑表，修改专辑里面声音数量值，+1
		//根据专辑id查询原始专辑信息
		AlbumInfo albumInfo = albumInfoMapper.selectById(trackInfoVo.getAlbumId());
		Integer includeTrackCount = albumInfo.getIncludeTrackCount();
		//专辑声音数量+1
		albumInfo.setIncludeTrackCount(includeTrackCount + 1);
		//调用更新方法
		albumInfoMapper.updateById(albumInfo);

	//3.添加声音四个统计数据，初始值0 track_stat
		this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_PLAY);
		this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_COLLECT);
		this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_PRAISE);
		this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_COMMENT);
}

	private void saveTrackStat(Long trackId, String statType) {
		//创建track_stat对象
		TrackStat trackStat = new TrackStat();
		trackStat.setTrackId(trackId);
		trackStat.setStatType(statType);
		trackStat.setStatNum(0);
		trackStatMapper.insert(trackStat);
	}


	@Override
	public IPage<TrackInfo> findUserTrackPage(Page<TrackInfo> pageParam, TrackInfoQuery trackInfoQuery) {

		return trackInfoMapper.selectUserTrackPage(pageParam,trackInfoQuery);

	}

	@Override
	public void removeTrackInfo(Long trackId) {
		//根据声音id获取专辑id
		TrackInfo trackInfo = trackInfoMapper.selectById(trackId);
		//根据声音id删除声音信息
		trackInfoMapper.deleteById(trackId);
		//修改声音所在专辑的声音数量 -1
		Long albumId = trackInfo.getAlbumId();
		//根据专辑id查询专辑数据，把数量-1，进行更新
		AlbumInfo albumInfo = albumInfoMapper.selectById(albumId);
		Integer includeTrackCount = albumInfo.getIncludeTrackCount();
		albumInfo.setIncludeTrackCount(includeTrackCount - 1);
		albumInfoMapper.updateById(albumInfo);
		//删除声音的统计数据
		LambdaQueryWrapper<TrackStat> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(TrackStat::getTrackId,trackId);
		trackStatMapper.delete(wrapper);
		//删除腾讯云上的声音文件
		//删除声音媒体文件
		vodService.removeTrack(trackInfo.getMediaFileId());
	}

	@Override
	public void updateTrackInfo(Long id, TrackInfoVo trackInfoVo) {
		TrackInfo trackInfo = this.getById(id);
		//媒体文件的唯一标识
		String mediaFileId = trackInfo.getMediaFileId();

		BeanUtils.copyProperties(trackInfoVo,trackInfo);

		if (!mediaFileId.equals(trackInfoVo.getMediaFileId())) {

			TrackMediaInfoVo trackMediaInfoVo = vodService.getmediaInfoByFileId(trackInfoVo.getMediaFileId());

			if (trackMediaInfoVo == null){
				throw new GuiguException(ResultCodeEnum.VOD_FILE_ID_ERROR);
			}
			trackInfo.setMediaDuration(trackMediaInfoVo.getDuration());
			trackInfo.setMediaSize(trackMediaInfoVo.getSize());
			trackInfo.setMediaType(trackMediaInfoVo.getType());
			trackInfo.setMediaUrl(trackMediaInfoVo.getMediaUrl());

			vodService.removeTrack(mediaFileId);
		}

		trackInfoMapper.updateById(trackInfo);
	}


}
